package com.ssyc.student.activity;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.utils.DateUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StGvAnswerAdapter;
import com.ssyc.student.bean.ChooseAnswerInfo;
import com.ssyc.student.fragment.StudentPrepareDetectionCommitFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 学生单--阅读--展示正确答案和错误答案的界面
 * Created by Administrator on 2018/5/10 0010.
 */

public class StudentShowTextAnswerActivity extends BaseActivity implements View.OnClickListener {

    //返回
    private ImageView ivBack;

    //总用时
    private TextView tvTime;

    //展示正确或者错误答案的GridView
    private GridView gv;

    //重做
//    private Button btRetry;

    //查看答案
    private Button btLookAnswer;


    //学生作答题目正确或者错误的集合Json字符串
    private String answerJson;

    //用于展示答题选项的数据集合
    private List<ChooseAnswerInfo> datas;

    //展示答题选项的列表适配器
    private StGvAnswerAdapter adapter;

    //获取第一次进入页面的key值
    public static final String FIRSTTIME = "firsttime";

    //第一次进入页面的时间
    private long firstTime;


    //查看答案的事件标识
    public static final int LOOKANSWER = 0x130;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_show_text_answer;
    }

    @Override
    protected void init() {

        showContent();

        initView();

        //获取页面数据
        initData();

        //设置总的用时时间
        setUseTime();

        //设置列表
        initGv();
    }


    /**
     * 初始化View的操作
     */
    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        tvTime = findViewById(R.id.tv_time);
        ivBack.setOnClickListener(this);
        gv = findViewById(R.id.gv);
//        btRetry = findViewById(R.id.bt_retry);
        btLookAnswer = findViewById(R.id.bt_look_answer);
//        btRetry.setOnClickListener(this);
        btLookAnswer.setOnClickListener(this);
    }

    /**
     * 获取页面数据的方法
     */
    private void initData() {
        answerJson = getIntent().getStringExtra("data");
        if (!TextUtils.isEmpty(answerJson)) {
            datas = GsonUtil.jsonToList(answerJson, ChooseAnswerInfo[].class);
        }
    }

    /**
     * 设置总的用时时间
     */
    private void setUseTime() {
        //获取传递过来的时间
        firstTime = getIntent().getLongExtra(StudentPrepareDetectionCommitFragment.FIRSTTIME, -1);
        //设置时间
        long time = System.currentTimeMillis() - firstTime;
        tvTime.setText("总用时： " + DateUtils.timeFormat(time));
    }


    /**
     * 设置展示正确或者错误答案的列表
     */
    private void initGv() {
        adapter = new StGvAnswerAdapter(this, datas, R.layout.student_rv_choose_answer);
        gv.setAdapter(adapter);
    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //返回
            setCheck();
        }
//        else if (id == R.id.bt_retry) {
//            //重试
//        }
        else if (id == R.id.bt_look_answer) {
            //查看答案,让答题选择答案界面的所有听力稿进行显示,同时让所有正确答案进行显示，同时该界面不可在进行交互

            setCheck();
        }

    }

    /**
     * 查看答案,返回
     */
    private void setCheck() {
        BusInfo busInfo = new BusInfo();
        busInfo.setType(LOOKANSWER);
        EventBus.getDefault().post(busInfo);
        finish();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode==KeyEvent.KEYCODE_BACK){
            setCheck();
        }

        return super.onKeyDown(keyCode, event);
    }
}
